
package com.xiaoma.myaudience.util.view.menu;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.util.Logger;

/**
 * 自定义菜单显示框.
 * 
 * @author wjying
 */
public class NeteaseMenu extends PopupWindow implements MenuBuilder.Callback{
    private final static String TAG = "NeteaseMenu";
    private int mFutureId = Window.FEATURE_OPTIONS_PANEL;

    private Activity mActivity;

    private MenuBuilder mMenuBuilder;

    private boolean mIsInExpandedMode;

    private View mParent;

    private MenuFrame mContent;

    public NeteaseMenu(Activity activity, MenuBuilder menu) {
        super(activity);
        mActivity = activity;
        mParent = activity.findViewById(android.R.id.content);
        mMenuBuilder = menu;
        mMenuBuilder.setCallback(this);

        mContent = new MenuFrame(activity);
        setContentView(mContent);

        setWidth(LayoutParams.FILL_PARENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setBackgroundDrawable(null);
        update();
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        final int keyCode = event.getKeyCode();
        final int action = event.getAction();
        
        if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) {
            if (mMenuBuilder.getVisibleItems() == null
                    || mMenuBuilder.getVisibleItems().size() == 0) {
                return false;
            }
            
            if (keyCode == KeyEvent.KEYCODE_MENU) {
                if (action == KeyEvent.ACTION_DOWN) {
                    toggle();
                }
                return true;
            } else if (isShowing() && keyCode == KeyEvent.KEYCODE_BACK) {
                if (mIsInExpandedMode) {
                    if (action == KeyEvent.ACTION_DOWN) {
                        reopen();
                    }

                } else {
                    if (action == KeyEvent.ACTION_DOWN) {
                        close(true);
                    }

                }
                return true;
            }
        }
        return false;
    }

    public Menu getMenu() {
        return mMenuBuilder;
    }

    public void reopen() {
        close(false);
        mIsInExpandedMode = !mIsInExpandedMode;
        open();
    }

    public void toggle() {
        if (isShowing()) {
            close(true);
        } else {
            open();
        }
    }

    public void open() {
        if (!mActivity.onPreparePanel(mFutureId, mContent, mMenuBuilder)
                || !mActivity.onMenuOpened(mFutureId, mMenuBuilder)) {
            return;
        }
        mContent.removeAllViews();

        View menuView = mMenuBuilder.getMenuView(mIsInExpandedMode ? MenuBuilder.TYPE_EXPANDED
                : MenuBuilder.TYPE_ICON, mContent);
        mContent.addView(menuView);
        setAnimationStyle(((MenuView) menuView).getWindowAnimations());
        update();
        showAtLocation(mParent, Gravity.BOTTOM, 0, 0);
    }

    //
    public void close(boolean allMenusAreClosing) {
        if (allMenusAreClosing) {
            mIsInExpandedMode = false;
        }
        dismiss();
    }

    @Override
    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
        close(allMenusAreClosing);
    }

    @Override
    public void onCloseSubMenu(SubMenuBuilder menu) {

    }

    @Override
    public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
        Logger.d(TAG + "onMenuItemSelected");
        return mActivity.onMenuItemSelected(mFutureId, item);
    }

    @Override
    public void onMenuModeChange(MenuBuilder menu) {
        reopen();
    }

    @Override
    public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
        Logger.d(TAG + "onSubMenuSelected");
        return false;
    }

    private class MenuFrame extends FrameLayout {

        public MenuFrame(Context context) {
            super(context);
            setBackgroundResource(R.drawable.menu_background_fill_parent_width);
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (NeteaseMenu.this.dispatchKeyEvent(event)) {
                return true;
            }
            return super.dispatchKeyEvent(event);
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            final int x = (int) event.getX();
            final int y = (int) event.getY();
            
            if ((event.getAction() == MotionEvent.ACTION_DOWN)
                    && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
                close(true);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                close(true);
                return true;
            } else {
                return super.onTouchEvent(event);
            }
        }

    }
}
